package com.htzos.main.tool;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import com.htzos.admin.system.bean.MenuTree;
import com.htzos.admin.system.bean.SystemRoleMenu;
import com.htzos.common.util.CommonUtil;

public class LoginTool {

	
	/**
	 * 根据当前用户生成其可访问的菜单路径列表,结果为一HASHMAP用于权限比对
	 * 
	 * @param request
	 * @param menuList
	 */
	public static void initRole(HttpSession session, List<SystemRoleMenu> menuList) {
		Map<String, String> roleMap = new HashMap<String, String>();
		session.setAttribute(CommonUtil.ROLE_MAP, roleMap);
		Iterator<SystemRoleMenu> it = menuList.iterator();
		SystemRoleMenu menu = null;
		while (it.hasNext()) {
			menu = it.next();
			roleMap.put(menu.getMenuUrl(),"");
			if (menu.getRadd() == 1) {
				String addUrl = menu.getAddUrl();
				if(null==addUrl){
					addUrl="";
				}
				String[] addUrls = addUrl.split(";");
				for (int i = 0; i < addUrls.length; i++) {
					if (null != addUrls[i] && addUrls[i].length() > 0) {
						roleMap.put(addUrls[i], "");
					}
				}

			}

			if (menu.getRdelete() == 1) {
				String deleteUrl = menu.getDeleteUrl();
				if(null==deleteUrl){
					deleteUrl="";
				}
				String[] deleteUrls = deleteUrl.split(";");
				for (int i = 0; i < deleteUrls.length; i++) {
					if (null != deleteUrls[i] && deleteUrls[i].length() > 0) {
						roleMap.put(deleteUrls[i], "");
					}
				}
			}

			if (menu.getRupdate() == 1) {
				String updateUrl = menu.getUpdateUrl();
				if(null==updateUrl){
					updateUrl="";
				}
				String[] updateUrls = updateUrl.split(";");
				for (int i = 0; i < updateUrls.length; i++) {
					if (null != updateUrls[i] && updateUrls[i].length() > 0) {
						roleMap.put(updateUrls[i], "");
					}
				}
			}

			if (menu.getRselect() == 1) {
				String selectUrl = menu.getSelectUrl();
				if(null==selectUrl){
					selectUrl="";
				}
				String[] selectUrls = selectUrl.split(";");
				for (int i = 0; i < selectUrls.length; i++) {
					if (null != selectUrls[i] && selectUrls[i].length() > 0) {
						roleMap.put(selectUrls[i], "");
					}
				}
			}
		}
	}
	
	/**
	 * 根据当前用户生成操作菜单JSON
	 * 
	 * @param menuList
	 * @return
	 */
	public static String getMenuString(List<SystemRoleMenu> menuList) {
		Map<String, List<SystemRoleMenu>> menuMap = new TreeMap<String, List<SystemRoleMenu>>();
		SystemRoleMenu menu = null;
		Iterator<SystemRoleMenu> iterator = menuList.iterator();
		while (iterator.hasNext()) {
			menu = iterator.next();
			// 上级菜单为空根菜单
			if (null == menu.getParentName()) {
				// 是根菜单将其录入系统
				menuMap.put(menu.getOrderBy() + ":" + menu.getMenuName() + ":" + menu.getMenuIcon(),
						new LinkedList<SystemRoleMenu>());
				menuMap.get(menu.getOrderBy() + ":" + menu.getMenuName() + ":" + menu.getMenuIcon()).add(
						menu);
			} else if (null != menu.getParentName()
					&& !menuMap.containsKey(menu.getParentOrderBy() + ":"
							+ menu.getParentName() + ":" + menu.getParentIcon())) {
				// 不是根菜单且上级菜单未录入系统
				menuMap.put(
						menu.getParentOrderBy() + ":" + menu.getParentName() + ":" + menu.getParentIcon(),
						new LinkedList<SystemRoleMenu>());
				menuMap.get(
						menu.getParentOrderBy() + ":" + menu.getParentName() + ":" + menu.getParentIcon())
						.add(menu);
			} else if (null != menu.getParentName()
					&& menuMap.containsKey(menu.getParentOrderBy() + ":"
							+ menu.getParentName() + ":" + menu.getParentIcon())) {
				// 不是根要菜单且上级菜单已经录入系统
				menuMap.get(
						menu.getParentOrderBy() + ":" + menu.getParentName() + ":" + menu.getParentIcon())
						.add(menu);
			}
		}

		Iterator<String> it = menuMap.keySet().iterator();
		List<MenuTree> list = new LinkedList<MenuTree>();
		MenuTree ml = null;
		String key;
		while (it.hasNext()) {
			ml = new MenuTree();
			key = it.next();
			String[] l = key.split(":");
			if(l.length == 1)
				continue;
			
			ml.setName(l[1]);
			
			if(l.length > 2)
				ml.setIcon(l[2]);
			ml.setValue(menuMap.get(key));
			list.add(ml);
		}

		return JSONArray.fromObject(list).toString();
	}

	/**
	 * 得到树状结构的菜单
	 *
	 * @author xudz
	 * @time 2017 2017年1月5日 下午2:04:04
	 * @parameter 
	 * @param menuList
	 * @return
	 */
	public static List<MenuTree> getMenuTree(List<SystemRoleMenu> menuList) {
		Map<String, List<SystemRoleMenu>> menuMap = new TreeMap<String, List<SystemRoleMenu>>();
		SystemRoleMenu menu = null;
		Iterator<SystemRoleMenu> iterator = menuList.iterator();
		while (iterator.hasNext()) {
			menu = iterator.next();
			// 上级菜单为空根菜单
			if (null == menu.getParentName()) {
				// 是根菜单将其录入系统
				menuMap.put(menu.getOrderBy() + ":" + menu.getMenuName() + ":" + menu.getMenuIcon(),new LinkedList<SystemRoleMenu>());
				menuMap.get(menu.getOrderBy() + ":" + menu.getMenuName() + ":" + menu.getMenuIcon()).add(menu);
			} else if (null != menu.getParentName()
					&& !menuMap.containsKey(menu.getParentOrderBy() + ":"
							+ menu.getParentName() + ":" + menu.getParentIcon())) {
				// 不是根菜单且上级菜单未录入系统
				menuMap.put(
						menu.getParentOrderBy() + ":" + menu.getParentName() + ":" + menu.getParentIcon(),
						new LinkedList<SystemRoleMenu>());
				menuMap.get(
						menu.getParentOrderBy() + ":" + menu.getParentName() + ":" + menu.getParentIcon())
						.add(menu);
			} else if (null != menu.getParentName()
					&& menuMap.containsKey(menu.getParentOrderBy() + ":"
							+ menu.getParentName() + ":" + menu.getParentIcon())) {
				// 不是根要菜单且上级菜单已经录入系统
				menuMap.get(
						menu.getParentOrderBy() + ":" + menu.getParentName() + ":" + menu.getParentIcon())
						.add(menu);
			}
		}

		Iterator<String> it = menuMap.keySet().iterator();
		List<MenuTree> list = new LinkedList<MenuTree>();
		MenuTree ml = null;
		String key;
		while (it.hasNext()) {
			ml = new MenuTree();
			key = it.next();
			String[] l = key.split(":");
			if(l.length == 1)
				continue;
			
			ml.setName(l[1]);
			
			if(l.length > 2)
				ml.setIcon(l[2]);
			ml.setValue(menuMap.get(key));
			list.add(ml);
		}
		return list;
	}
}
